﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace Zo.Json
{
    public class JsonManager
    {
        /// <summary>
        /// 将对象转换成 Json 字符串。
        /// </summary>
        /// <param name="data">要转换的对象。</param>
        /// <returns>Json 字符串。</returns>
        public static string Serialize(object data)
        {
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 将 Json 字符串转换成对象。
        /// </summary>
        /// <param name="json">Json 字符串。</param>
        /// <returns>对象。</returns>
        public static IDictionary<string, string> DeserializeDictionary(string json)
        {
            return JsonConvert.DeserializeObject<IDictionary<string, string>>(json);
        }

        /// <summary>
        /// 将 Json 字符串转换成对象。
        /// </summary>
        /// <typeparam name="TSource">对象类型。</typeparam>
        /// <param name="json">Json 字符串。</param>
        /// <returns>对象。</returns>
        public static TSource Deserialize<TSource>(string json) where TSource : new()
        {
            return JsonConvert.DeserializeObject<TSource>(json);
        }

        /// <summary>
        /// 将 Json 字符串转换成对象。
        /// </summary>
        /// <param name="json">Json 字符串。</param>
        /// <param name="type">对象类型。</param>
        /// <returns>对象。</returns>
        public static object DeserializeObject(string json, Type type)
        {
            return JsonConvert.DeserializeObject(json, type);
        }
    }
}